package com.meihuayishu.backend.service.impl;

import com.meihuayishu.backend.entity.Hexagram;
import com.meihuayishu.backend.entity.YaoCi;
import com.meihuayishu.backend.repository.HexagramRepository;
import com.meihuayishu.backend.repository.YaoCiRepository;
import com.meihuayishu.backend.service.HexagramService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Slf4j
public class HexagramServiceImpl implements HexagramService {
    
    private final HexagramRepository hexagramRepository;
    private final YaoCiRepository yaoCiRepository;
    
    @Override
    public Optional<Hexagram> getHexagramByCode(String code) {
        try {
            return hexagramRepository.findByCode(code);
        } catch (Exception e) {
            log.error("根据卦象编码获取卦象失败: {}", code, e);
            return Optional.empty();
        }
    }
    
    @Override
    public Optional<Hexagram> getHexagramByName(String name) {
        try {
            return hexagramRepository.findByName(name);
        } catch (Exception e) {
            log.error("根据卦名获取卦象失败: {}", name, e);
            return Optional.empty();
        }
    }
    
    @Override
    public Optional<Hexagram> getHexagramBySymbol(String symbol) {
        try {
            // 直接使用新的方法获取所有匹配的记录，避免唯一性约束问题
            List<Hexagram> hexagrams = hexagramRepository.findAllBySymbol(symbol);
            if (!hexagrams.isEmpty()) {
                // 如果有多个匹配的记录，返回第一个
                if (hexagrams.size() > 1) {
                    log.warn("发现重复的卦象符号: {}，返回第一个匹配的记录", symbol);
                }
                return Optional.of(hexagrams.get(0));
            }
            
            return Optional.empty();
        } catch (Exception e) {
            log.error("根据卦象符号获取卦象失败: {}", symbol, e);
            return Optional.empty();
        }
    }
    
    @Override
    public List<Hexagram> getAllHexagrams() {
        try {
            return hexagramRepository.findAll();
        } catch (Exception e) {
            log.error("获取所有卦象失败", e);
            return List.of();
        }
    }
    
    @Override
    public List<YaoCi> getYaoCiByHexagramId(Long hexagramId) {
        try {
            return yaoCiRepository.findByHexagramId(hexagramId);
        } catch (Exception e) {
            log.error("根据卦象ID获取爻辞失败: {}", hexagramId, e);
            return List.of();
        }
    }
    
    @Override
    public YaoCi getYaoCiByHexagramIdAndPosition(Long hexagramId, Integer yaoPosition) {
        try {
            return yaoCiRepository.findByHexagramIdAndYaoPosition(hexagramId, yaoPosition);
        } catch (Exception e) {
            log.error("根据卦象ID和爻位获取爻辞失败: {}, {}", hexagramId, yaoPosition, e);
            return null;
        }
    }
    
    @Override
    public List<Hexagram> searchHexagrams(String keyword) {
        try {
            return hexagramRepository.searchByKeyword(keyword);
        } catch (Exception e) {
            log.error("搜索卦象失败: {}", keyword, e);
            return List.of();
        }
    }
    
    @Override
    public List<Hexagram> getHexagramsByFiveElements(String fiveElements) {
        try {
            return hexagramRepository.findByFiveElements(fiveElements);
        } catch (Exception e) {
            log.error("根据五行属性获取卦象失败: {}", fiveElements, e);
            return List.of();
        }
    }
}